package com.crazecoder.flutter.iflyad.utils;

import android.app.Activity;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.iflytek.voiceads.IFLYInterstitialAd;
import com.iflytek.voiceads.IFLYVideoAd;
import com.iflytek.voiceads.config.AdError;
import com.iflytek.voiceads.config.AdKeys;
import com.iflytek.voiceads.conn.VideoDataRef;
import com.iflytek.voiceads.listener.IFLYVideoListener;

import io.flutter.Log;

public class VideoAdUtil implements IFLYVideoListener {
    private static VideoAdUtil instance;
    private Activity activity;
    private IFLYVideoAd videoAd;
    private ViewGroup adView;

    private VideoAdUtil(Activity activity) {
        this.activity = activity;
    }

    public static VideoAdUtil getInstance(Activity activity) {
        if (instance == null) {
            instance = new VideoAdUtil(activity);
        }
        return instance;
    }

    public void show(String adUnitId) {
        // IFLYVideoAd.NATIVE_VIDEO_AD 信息流视频广告
        // IFLYVideoAd.FULLSCREEN_VIDEO_AD 全屏视频广告，PORTRAIT竖屏展示，LANDSCAPE横屏展示

        //创建视频广告：adUnitId：开发者在讯飞AI营销云平台(http://www.voiceads.cn/)申请的视频广告位 ID
        videoAd = new IFLYVideoAd(activity.getApplicationContext(), adUnitId, IFLYVideoAd.FULLSCREEN_VIDEO_AD, this);

        //设置oaid，获取方式见 -> 4.常见问题
        videoAd.setParameter(AdKeys.OAID, OAIDUtil.getInstance(activity).getOAID());

        //请求广告
        videoAd.loadAd();

    }

    public void dispose() {
        if (videoAd != null) {
            videoAd.release();
            adView = null;
            videoAd = null;
        }

    }
    private void play(){
        LinearLayout content = new LinearLayout(activity);
        content.setOrientation(LinearLayout.VERTICAL);
        content.setGravity(Gravity.CENTER);
        content.removeAllViews();
        adView = videoAd.getVideoView();
        adView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        content.addView(adView);
        activity.addContentView(
                content,
                new ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        videoAd.showAd(IFLYVideoAd.FULLSCREEN_VIDEO_AD);
    }

    @Override
    public void onAdLoaded(VideoDataRef videoDataRef) {
        // 广告请求成功
        // 开始缓存视频
        Log.d("Ad_Android_Demo", "request success");
        play();
    }

    @Override
    public void onAdFailed(AdError error) {
        // 广告请求失败
        Log.d("Ad_Android_Demo", "onAdFailed "+error.getErrorDescription());
    }


    @Override
    public void onVideoCached() {
        Log.d("Ad_Android_Demo", "onVideoCached");

        // 视频广告缓存成功
        // 添加视频广告并准备播放
        //当为IFLYVideoAd.NATIVE_VIDEO_AD,调用如下接口
//        videoAd.showAd();

        //当为IFLYVideoAd.FULLSCREEN_VIDEO_AD,调用如下接口
    }

    @Override
    public void onAdClick() {
        // 点击广告，这里需调用 onClicked()接口
    }

    @Override
    public void onVideoStart() {
        // 广告缓冲完成，开始播放，这里需调用 onExposured()接口
        Log.d("Ad_Android_Demo", "onVideoStart ");

    }

    @Override
    public void onVideoComplete() {
        // 广告播放完成
        Log.d("Ad_Android_Demo", "onVideoComplete ");

    }

    @Override
    public void onVideoReplay() {
        // 广告重新播放
    }

    @Override
    public void onAdPlayError() {
        // 广告播放出错
        Log.d("Ad_Android_Demo", "onAdPlayError ");
    }

    @Override
    public void onCancel() {
        // 下载类广告，下载提示框 “取消”
    }

    @Override
    public void onConfirm() {
        // 下载类广告，下载提示框 “确认”
        Log.d("Ad_Android_Demo", "onConfirm ");
    }
}
